package com.example.airole.common.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

/**
 * 统一响应结果封装
 * @param <T> 数据类型
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseResponse<T> {
    
    /**
     * 响应码
     */
    private Integer code;
    
    /**
     * 响应消息
     */
    private String message;
    
    /**
     * 响应数据
     */
    private T data;
    
    /**
     * 时间戳
     */
    private Long timestamp;
    
    public BaseResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }
    
    public BaseResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }
    
    /**
     * 成功响应
     */
    public static <T> BaseResponse<T> success() {
        return new BaseResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
    }
    
    /**
     * 成功响应带数据
     */
    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }
    
    /**
     * 成功响应带消息和数据
     */
    public static <T> BaseResponse<T> success(String message, T data) {
        return new BaseResponse<>(ResponseCode.SUCCESS.getCode(), message, data);
    }
    
    /**
     * 失败响应
     */
    public static <T> BaseResponse<T> error() {
        return new BaseResponse<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage());
    }
    
    /**
     * 失败响应带消息
     */
    public static <T> BaseResponse<T> error(String message) {
        return new BaseResponse<>(ResponseCode.ERROR.getCode(), message);
    }
    
    /**
     * 失败响应带响应码和消息
     */
    public static <T> BaseResponse<T> error(Integer code, String message) {
        return new BaseResponse<>(code, message);
    }
    
    /**
     * 失败响应带响应码、消息和数据
     */
    public static <T> BaseResponse<T> error(Integer code, String message, T data) {
        return new BaseResponse<>(code, message, data);
    }
    
    /**
     * 自定义响应
     */
    public static <T> BaseResponse<T> result(ResponseCode responseCode) {
        return new BaseResponse<>(responseCode.getCode(), responseCode.getMessage());
    }
    
    /**
     * 自定义响应带数据
     */
    public static <T> BaseResponse<T> result(ResponseCode responseCode, T data) {
        return new BaseResponse<>(responseCode.getCode(), responseCode.getMessage(), data);
    }
}

